function HandleBackendUser(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._createElements();
};

HandleBackendUser.prototype._createElements = function() {
  this._loadData();
};

HandleBackendUser.prototype._loadData = function() {
  this._user = null;
  this._retrieveUser();
};

HandleBackendUser.prototype._verifyData = function() {
  if (this._user) {
    this._updateElements();
  }
};

HandleBackendUser.prototype._retrieveUser = function(page, condition, date, from, to, pos) {
  var _self = this;
  if (this._options.id) {
    /*new RequestUtils()._read('user', null, 'd.oid = ' + this._options.id, null, null, null, null, function(result, params) { _self._user = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                             _self._verifyData.call(_self);
                                                                                                                           }, null);
 */
   var args = '&oid=' + this._options.id; 
  new RequestUtils()._mysql('v1user', args, function(result, params) { _self._user = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
  } else {
    this._user = v1user.instance();
    this._verifyData();
  }
};

HandleBackendUser.prototype._updateElements = function() {
  var table, tr, td, _self = this;
  DlmanageUtils.clear(this._gui);
  
  /*if (!this._user.skill) {
    this._user.skill = UserSkill.instance();
  }*/
  
  this._gui.type.options[this._gui.type.options.length] = new Option('');
  for (var i = 0, il = UserType.array().length; i < il; i++) {
    var type = UserType.array()[i];
    if (type.charAt(0) != '#') {
      continue;
    }
    option = new Option(type.substring(1));
    option._type = type;
    this._gui.type.options[this._gui.type.options.length] = option;
    if (type == this._user.type) {
      this._gui.type.selectedIndex = this._gui.type.options.length - 1;
    }
  }

  this._gui.account.value = this._user.account;
  this._gui.email.value = this._user.email;
  this._gui.name.value = this._user.name;
  this._gui.password.value = this._user.password;

  this._gui.photographer.enabled.checked = this._user.pEnabled == 0 ? '' : this._user.pEnabled;
//  this._gui.photographer.pCusPhoto.checked = this._user.skill.pCusPhoto;
  this._gui.videographer.enabled.checked = this._user.vEnabled == 0 ? '' : this._user.vEnabled;
  this._gui.makeup.enabled.checked = this._user.mEnabled == 0 ? '' : this._user.mEnabled;
  this._gui.lighting.enabled.checked = this._user.lEnabled == 0 ? '' : this._user.lEnabled;

  this._gui.photographer.pWedding.value = this._user.pWedding;
  this._gui.photographer.pPrewedding.value = this._user.pPrewedding;
  this._gui.photographer.pPortrait.value = this._user.pPortrait;
  this._gui.photographer.pEngagement.value = this._user.pEngagement;
//  this._gui.photographer.ftpHost.value = this._user.ftpHost;
//  this._gui.photographer.ftpAccount.value = this._user.ftpAccount;
//  this._gui.photographer.ftpPassword.value = this._user.ftpPassword;
  
  this._gui.videographer.vWedding.value = this._user.vWedding;
  this._gui.videographer.vPrewedding.value = this._user.vPrewedding;
  this._gui.videographer.vPortrait.value = this._user.vPortrait;
  this._gui.videographer.vEngagement.value = this._user.vEngagement;
  this._gui.makeup.mHouse.value = this._user.mHouse;
  this._gui.makeup.mStudio.value = this._user.mStudio;
  this._gui.makeup.mFullDay.value = this._user.mFullDay;
  this._gui.lighting.rate.value = this._user.lightingRate;

  if (this._user.pEnabled == 1) {
    this._gui.photographer.pWedding.disabled = false;
    this._gui.photographer.pPrewedding.disabled = false;
    this._gui.photographer.pPortrait.disabled = false;
    this._gui.photographer.pEngagement.disabled = false;
//    this._gui.photographer.pCusPhoto.disabled = false;
  } else {
    this._gui.photographer.pWedding.disabled = true;
    this._gui.photographer.pPrewedding.disabled = true;
    this._gui.photographer.pPortrait.disabled = true;
    this._gui.photographer.pEngagement.disabled = true;
//    this._gui.photographer.pCusPhoto.disabled = true;
  }
//  if (this._user.pCusPhoto) {
//    this._gui.photographer.ftpHost.disabled = false;
//    this._gui.photographer.ftpAccount.disabled = false;
//    this._gui.photographer.ftpPassword.disabled = false;
//  } else {
//    this._gui.photographer.ftpHost.disabled = true;
//    this._gui.photographer.ftpAccount.disabled = true;
//    this._gui.photographer.ftpPassword.disabled = true;
//  }
//  
  if (this._user.vEnabled == 1) {
    this._gui.videographer.vWedding.disabled = false;
    this._gui.videographer.vPrewedding.disabled = false;
    this._gui.videographer.vPortrait.disabled = false;
    this._gui.videographer.vEngagement.disabled = false;
  } else {
    this._gui.videographer.vWedding.disabled = true;
    this._gui.videographer.vPrewedding.disabled = true;
    this._gui.videographer.vPortrait.disabled = true;
    this._gui.videographer.vEngagement.disabled = true;
  }
  if (this._user.mEnabled == 1) {
    this._gui.makeup.mHouse.disabled = false;
    this._gui.makeup.mStudio.disabled = false;
    this._gui.makeup.mFullDay.disabled = false;
  } else {
    this._gui.makeup.mHouse.disabled = true;
    this._gui.makeup.mStudio.disabled = true;
    this._gui.makeup.mFullDay.disabled = true;
  }
  if (this._user.lEnabled == 1) {
    this._gui.lighting.rate.disabled = false;
  } else {
    this._gui.lighting.rate.disabled = true;
  }
  if (this._user.employmentType == 0) {
    this._gui.noneRadio.checked = true;
  } else if (this._user.employmentType == 1) {
    this._gui.fulltimeRadio.checked = true;
    var table = document.createElement('table');
    this._gui.inputDiv.appendChild(table);
    var tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.fontWeight = 'bold';
    td.style.fontSize = '110%';
    td.style.color = '#776E64';
    td.appendChild(document.createTextNode('Yearly Rate ($)'));
    td = tr.insertCell(-1);
    var input = DOMUtils.createInput('', '', 'text', this._user.yearlyRate, null);
    input.onchange = function() {_self._user.yearlyRate = this.value; };
    input.style.borderLeft='solid 1px #d2c4b9';
    input.style.borderTop= 'solid 1px #d2c4b9';
    input.style.borderRight= 'solid 1px #fff';
    input.style.borderBottom= 'solid 1px #fff';
    td.appendChild(input);
    
    td = tr.insertCell(-1);
    td.style.fontWeight = 'bold';
    td.style.fontSize = '110%';
    td.style.color = '#776E64';
    td.appendChild(document.createTextNode('Commission Rate ($)'));
    td = tr.insertCell(-1);
    var input = DOMUtils.createInput('', '', 'text', this._user.commissionRate, null);
    input.onchange = function() {_self._user.commissionRate = this.value; };
    input.style.borderLeft='solid 1px #d2c4b9';
    input.style.borderTop= 'solid 1px #d2c4b9';
    input.style.borderRight= 'solid 1px #fff';
    input.style.borderBottom= 'solid 1px #fff';
    td.appendChild(input);
  } else {
    this._gui.parttimeRadio.checked = true;
    var table = document.createElement('table');
    this._gui.inputDiv.appendChild(table);
    var tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.fontWeight = 'bold';
    td.style.fontSize = '110%';
    td.style.color = '#776E64';
    td.appendChild(document.createTextNode('Hourly Rate ($)'));
    td = tr.insertCell(-1);
    var input = DOMUtils.createInput('', '', 'text', this._user.hourlyRate, null);
    input.onchange = function() {_self._user.hourlyRate = this.value; };
    input.style.borderLeft='solid 1px #d2c4b9';
    input.style.borderTop= 'solid 1px #d2c4b9';
    input.style.borderRight= 'solid 1px #fff';
    input.style.borderBottom= 'solid 1px #fff';
    td.appendChild(input);
  }
  
  this._gui.noneRadio.onchange = function() { if (_self._gui.noneRadio.checked) {
                                                _self._user.employmentType = 0;
                                                _self._updateElements.call(_self);
                                              }
                                            };
  this._gui.fulltimeRadio.onchange = function() { if (_self._gui.fulltimeRadio.checked) {
                                                    _self._user.employmentType = 1;
                                                    _self._updateElements.call(_self);
                                                  } else {
                                                    _self._updateElements.call(_self);
                                                  }
                                                };
  this._gui.parttimeRadio.onchange = function() { if (_self._gui.parttimeRadio.checked) {
                                                    _self._user.employmentType = 2;
                                                    _self._updateElements.call(_self);
                                                  } else {
                                                    _self._updateElements.call(_self);
                                                  }
                                                };
  this._gui.account.onchange = function() { _self._user.account = this.value; };
  this._gui.email.onchange = function() { _self._user.email = this.value; };
  this._gui.name.onchange = function() { _self._user.name = this.value; };
  this._gui.password.onchange = function() { _self._user.password = this.value; };
  this._gui.type.onchange = function() { _self._user.type = (this.options[this.selectedIndex]._type) ? this.options[this.selectedIndex]._type : ''; };
  this._gui.photographer.enabled.onchange = function() { if (_self._gui.photographer.enabled.checked) {
                                                            _self._user.pEnabled = true;
                                                            _self._updateElements.call(_self);
                                                          } else {
                                                            _self._user.pEnabled = false;
//                                                            _self._user.pCusPhoto = false;
                                                            _self._user.pWedding = 0;
                                                            _self._user.pPrewedding = 0;
                                                            _self._user.pPortrait = 0;
                                                            _self._user.pEngagement = 0;
//                                                            _self._user.ftpHost = '';
//                                                            _self._user.ftpAccount = '';
//                                                            _self._user.ftpPassword = '';
                                                            _self._updateElements.call(_self);
                                                          }
                                                        };
//  this._gui.photographer.pCusPhoto.onchange = function() { if (_self._gui.photographer.pCusPhoto.checked) {
//                                                              _self._user.pCusPhoto = true;
//                                                              _self._updateElements.call(_self);
//                                                            } else {
//                                                              _self._user.pCusPhoto = false;
//                                                              _self._user.ftpHost = '';
//                                                              _self._user.ftpAccount = '';
//                                                              _self._user.ftpPassword = '';
//                                                              _self._updateElements.call(_self);
//                                                            }
//                                                          };
//                                                     
  this._gui.videographer.enabled.onchange = function() { if (_self._gui.videographer.enabled.checked) {
                                                            _self._user.vEnabled = true;
                                                            _self._updateElements.call(_self);
                                                          } else {
                                                            _self._user.vEnabled = false;
                                                            _self._user.vWedding = 0;
                                                            _self._user.vPrewedding = 0;
                                                            _self._user.vPortrait = 0;
                                                            _self._user.vEngagement = 0;
                                                            _self._updateElements.call(_self);
                                                          }
                                                        };
  this._gui.makeup.enabled.onchange = function() { if (_self._gui.makeup.enabled.checked) {
                                                     _self._user.mEnabled = true;
                                                     _self._updateElements.call(_self);
                                                   } else {
                                                     _self._user.mEnabled = false;
                                                     _self._user.mHouse = 0; 
                                                     _self._user.mStudio = 0;
                                                     _self._user.mFullDay = 0;
                                                     _self._updateElements.call(_self);
                                                   }
                                                 };
  this._gui.lighting.enabled.onchange = function() { if (_self._gui.lighting.enabled.checked) {
                                                       _self._user.lEnabled = true;
                                                       _self._updateElements.call(_self);
                                                     } else {
                                                       _self._user.lEnabled = false;
                                                       _self._user.lightingRate = 0;
                                                       _self._updateElements.call(_self);
                                                     }
                                                   };
  this._gui.photographer.pWedding.onchange = function() { _self._user.pWedding = this.value; };
  this._gui.photographer.pPrewedding.onchange = function() { _self._user.pPrewedding = this.value; };
  this._gui.photographer.pPortrait.onchange = function() { _self._user.pPortrait = this.value; };
  this._gui.photographer.pEngagement.onchange = function() { _self._user.pEngagement = this.value; };
//  this._gui.photographer.ftpHost.onchange = function() { _self._user.skill.ftpHost = this.value; };
//  this._gui.photographer.ftpAccount.onchange = function() { _self._user.skill.ftpAccount = this.value; };
//  this._gui.photographer.ftpPassword.onchange = function() { _self._user.skill.ftpPassword = this.value; };
  
  this._gui.videographer.vWedding.onchange = function() { _self._user.vWedding = this.value; };
  this._gui.videographer.vPrewedding.onchange = function() { _self._user.vPrewedding = this.value; };
  this._gui.videographer.vPortrait.onchange = function() { _self._user.vPortrait = this.value; };
  this._gui.videographer.vEngagement.onchange = function() { _self._user.vEngagement = this.value; };
  this._gui.makeup.mHouse.onchange = function() { _self._user.mHouse = this.value; };
  this._gui.makeup.mStudio.onchange = function() { _self._user.mStudio = this.value; };
  this._gui.makeup.mFullDay.onchange = function() { _self._user.mFullDay = this.value; };
  this._gui.lighting.rate.onchange = function() { _self._user.lightingRate = this.value; };

  this._gui.save.onclick = function() { new RequestUtils()._mwrite('v1user', [_self._user], [], function() { location.href = '?p=pagefactory&t=pagebackendusers&m=' + JSON.stringify({ a: 2, b: 1 }); }, null); };
  this._gui.cancel.onclick = function() { history.back(); };
};
